﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Oplaco.Core.BusinessLogicLayer;
using System.Configuration;

public partial class MyShop_MyAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

 
    }

    

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MembershipUser mu = Membership.GetUser(txtEmail.Text);
            MembersDB MDB = new MembersDB();
            MDB.Initialize(txtEmail.Text);
            string oldpassword = MDB.Password;
            string newpassword = this.GetRandomPasswordUsingGUID(6);
            mu.ChangePassword(oldpassword, newpassword);

            Mail.Send(txtEmail.Text, "Oplaco message", "You password has been changed to " + newpassword);
        }
        catch (Exception exception) {
            CustomValidator val = new CustomValidator();
            val.IsValid = false;
            val.ErrorMessage = ConfigurationManager.AppSettings["GenericErrorMessage"];
            val.ValidationGroup = "ValGroupAccount";
            this.Page.Validators.Add(val);
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
        }
    }

    private string GetRandomPasswordUsingGUID(int length)
    {
        // Get the GUID
        string guidResult = System.Guid.NewGuid().ToString();

        // Remove the hyphens
        guidResult = guidResult.Replace("-", string.Empty);

        // Make sure length is valid
        if (length <= 0 || length > guidResult.Length)
            throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

        // Return the first length bytes
        return guidResult.Substring(0, length);
    }
}